package com.supreme.cacheserver.service;

import com.supreme.common.dto.OmsDept;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

@Service
public class ReflectService {

    public long showOmsDeptList(List<OmsDept> omsDeptList) {
        StopWatch stopWatch = new StopWatch("showOmsDeptList");
        stopWatch.start();

        for (OmsDept omsDept : omsDeptList) {
            System.out.println("omsDept.getDeptId() = " + omsDept.getDeptId());
            System.out.println("omsDept.getDeptName() = " + omsDept.getDeptName());
            System.out.println("omsDept.getDeptLevel() = " + omsDept.getDeptLevel());
            System.out.println("omsDept.getStoreCode() = " + omsDept.getStoreCode());
            System.out.println("omsDept.getParentStoreCode() = " + omsDept.getParentStoreCode());
            System.out.println("omsDept.getStatus() = " + omsDept.getStatus());
            System.out.println("omsDept.getSyncType() = " + omsDept.getSyncType());
            System.out.println("omsDept.getAncestors() = " + omsDept.getAncestors());
            System.out.println("omsDept.getCodeAncestors() = " + omsDept.getCodeAncestors());
            System.out.println("omsDept.getEmail() = " + omsDept.getEmail());
            System.out.println("omsDept.getLeader() = " + omsDept.getLeader());
            System.out.println("omsDept.getCreateBy() = " + omsDept.getCreateBy());
            System.out.println("omsDept.getCreateTime() = " + omsDept.getCreateTime());
            System.out.println("omsDept.getUpdateTime() = " + omsDept.getUpdateTime());
            System.out.println("omsDept.getOrderNum() = " + omsDept.getOrderNum());
            System.out.println("omsDept.getPhone() = " + omsDept.getPhone());
            System.out.println("omsDept.getRemark() = " + omsDept.getRemark());
        }

        stopWatch.stop();
        return stopWatch.getTotalTimeMillis();
    }

    public long showReflectFieldList(List<Object> objectList, String packageName) {
        StopWatch stopWatch = new StopWatch("showObjectList");
        stopWatch.start();

        Class<?> clazz;
        try {
            clazz = Class.forName(packageName);

            for (Object objectItem : objectList) {
                for (Field declaredField : clazz.getDeclaredFields()) {
                    declaredField.setAccessible(true);
                    Object value = declaredField.get(objectItem);
                    String name = declaredField.getName();
//                    Object value = FieldUtils.getField(objectItem.getClass(), name, true);
//                    Object value = FieldUtils.readField(objectItem, name, true);
                    System.out.println(name + " = " + value);
                }
            }
        } catch (ClassNotFoundException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }

        stopWatch.stop();
        return stopWatch.getTotalTimeMillis();
    }

    public long showReflectMethodList(List<Object> objectList, String packageName) {
        StopWatch stopWatch = new StopWatch("showObjectList");
        stopWatch.start();

        Class<?> clazz;
        try {
            clazz = Class.forName(packageName);

            for (Object objectItem : objectList) {
                Method getDeptName = clazz.getMethod("getDeptName");
                String deptName = (String) getDeptName.invoke(objectItem);
                System.out.println("deptName = " + deptName);
            }
        } catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            throw new RuntimeException(e);
        }

        stopWatch.stop();
        return stopWatch.getTotalTimeMillis();
    }
}
